/**
 * axiosInstance的请求实例
 */

import axios from "axios";
import router from "../router";
import { mainStore } from "../store";
//创建axios新实例工厂
const axiosInstance = axios.create({
    baseURL: baseURL,
    timeout: 5000
});

//设置请求拦截，每次登陆请求前携带之前登陆的token
axiosInstance.interceptors.request.use(config => {
    const store = mainStore();
    config.headers["softeem_ele_admin_token"] = store.token;
    return config;
});

//设置响应 interceptors拦截器
axiosInstance.interceptors.response.use(resp => {
    if (resp.data.status === "success") {
        console.log(resp);
        return Promise.resolve(resp.data.data);
    } 
    else {
        console.log("服务器错误", resp.data.data);
        return Promise.reject(resp.data.data);
    }
}, error => {
    console.log("服务器请求错误", error);
    if(error.response.status===403){
        router.replace({name:'Login'})
    }
    return Promise.reject(error);
})
export default axiosInstance;

/**
 * const axiosInstance=axios.create
 * 
 * 
 */